package com.one.efaimaly.main.ui.binder;

import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.one.common.manager.imageloader.LoaderManager;
import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.one.efaimaly.R;
import com.one.common.model.bean.BannerBean;
import com.one.efaimaly.main.model.item.BannerItem;

import static com.one.common.model.http.NetConstant.IMG_HOST;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/8/5 下午7:19
 * description ：banner
 **/
public class BannerBinder extends BaseItemBinder<BannerItem> implements OnItemClickListener {
    public BannerBinder(OnCreateBanner createBanner) {
        super(R.layout.item_banner);
        this.onCreateBanner = createBanner;
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, @NonNull BannerItem item) {
        ConvenientBanner<BannerBean> convenientBanner = holder.getView(R.id.banner);

        convenientBanner.setPages(
                new CBViewHolderCreator() {
                    @Override
                    public LocalImageHolderView createHolder(View itemView) {
                        return new LocalImageHolderView(itemView);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.layout_banner;
                    }
                }, item.getBanners())
                .setOnItemClickListener(this);
        onCreateBanner.onCreateView(convenientBanner);
        if (!convenientBanner.isTurning()) {
            convenientBanner.startTurning(2000);
        }
    }

    @Override
    public void onItemClick(int position) {
        // TODO 点击事件
    }

    public class LocalImageHolderView extends Holder<BannerBean> {

        ImageView imageView;

        public LocalImageHolderView(View itemView) {
            super(itemView);
        }

        @Override
        protected void initView(View itemView) {
            imageView = (ImageView) itemView;
        }

        @Override
        public void updateUI(BannerBean data) {

            LoaderManager.getLoader().loadNet(imageView, IMG_HOST+data.getBanner_img());
        }
    }

    private OnCreateBanner onCreateBanner;

    public interface OnCreateBanner {
        void onCreateView(ConvenientBanner convenientBanner);
    }


}
